/** 
 * https://leetcode.cn/problems/max-consecutive-ones-iii/description/
 * 1004. 最大连续1的个数 III
 * medium, 王顶成 2024.9.4
 * c++  排序+滑动窗口
 */
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int ans = 0, l = 0, c = 0;
        for (int r = 0; r < nums.size(); r++) {
            c += 1 - nums[r]; 
            while (c > k) {
                c -= 1 - nums[l++];
            }
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};